package com.a0soft.gphone.aTruffleHog.pref;

import java.util.ArrayList;

import com.a0soft.gphone.aTruffleHog.util.Wrapper;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCPUFormFrg;
import com.a0soft.gphone.aTruffleHog.wnd.stat.FindDevicesByCameraFormFrg;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class Pref {
    private static final String KEY_AGREE_AGREEMENT = "aa";
    private static final boolean KEY_AGREE_AGREEMENT_DEFAULT = false;
    
    private static final String KEY_MIN_CAMERA_PIXELS = "cp_min";
    private static final String KEY_MAX_CAMERA_PIXELS = "cp_max";
    
    private static final String KEY_MIN_CORE_NUM = "cn_min";
    private static final String KEY_MAX_CORE_NUM = "cn_max";
    private static final String KEY_MIN_CPU_SPEED = "cs_min";
    private static final String KEY_MAX_CPU_SPEED = "cs_max";
    
    private static final String KEY_OTHER_DEVICES_NUM = "odn";
    private static final String KEY_OTHER_DEVICES_MID_PREFIX = "odmid_";
    private static final String KEY_OTHER_DEVICES_MODEL_PREFIX = "odm_";

    private static final String FILE_NAME = "pref";
    
    public static boolean
    IsAgreeAgreement(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(KEY_AGREE_AGREEMENT, 
                               KEY_AGREE_AGREEMENT_DEFAULT);
    }
    
    public static void
    SetAgreeAgreement(Context ctx, boolean bAgree)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putBoolean(KEY_AGREE_AGREEMENT, bAgree);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static int
    GetMinCameraPixelsIdx(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getInt(KEY_MIN_CAMERA_PIXELS, 
                           FindDevicesByCameraFormFrg.CAMERA_PIXELS_IDX_MIN);
    }
    
    public static void
    SetMinCameraPixelsIdx(Context ctx, int nMin)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putInt(KEY_MIN_CAMERA_PIXELS, nMin);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static int
    GetMaxCameraPixelsIdx(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getInt(KEY_MAX_CAMERA_PIXELS, 
                           FindDevicesByCameraFormFrg.CAMERA_PIXELS_IDX_MAX);
    }
    
    public static void
    SetMaxCameraPixelsIdx(Context ctx, int nMax)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putInt(KEY_MAX_CAMERA_PIXELS, nMax);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static int
    GetMinCoreNumIdx(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getInt(KEY_MIN_CORE_NUM, 
                           FindDevicesByCPUFormFrg.CORE_NUM_IDX_MIN);
    }
    
    public static void
    SetMinCoreNumIdx(Context ctx, int nMin)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putInt(KEY_MIN_CORE_NUM, nMin);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static int
    GetMaxCoreNumIdx(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getInt(KEY_MAX_CORE_NUM, 
                           FindDevicesByCPUFormFrg.CORE_NUM_IDX_MAX);
    }
    
    public static void
    SetMaxCoreNumIdx(Context ctx, int nMax)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putInt(KEY_MAX_CORE_NUM, nMax);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static int
    GetMinCpuSpeedIdx(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getInt(KEY_MIN_CPU_SPEED, 
                           FindDevicesByCPUFormFrg.CPU_SPEED_IDX_MIN);
    }
    
    public static void
    SetMinCpuSpeedIdx(Context ctx, int nMin)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putInt(KEY_MIN_CPU_SPEED, nMin);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static int
    GetMaxCpuSpeedIdx(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return pref.getInt(KEY_MAX_CPU_SPEED, 
                           FindDevicesByCPUFormFrg.CPU_SPEED_IDX_MAX);
    }
    
    public static void
    SetMaxCpuSpeedIdx(Context ctx, int nMax)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        
        ed.putInt(KEY_MAX_CPU_SPEED, nMax);
        
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static ArrayList<String>
    GetOtherDevices(Context ctx)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        final int N = pref.getInt(KEY_OTHER_DEVICES_NUM, 0);
        ArrayList<String> strDevices = new ArrayList<String>();
        if (N > 0) {
            for (int i = 0; i < N; i++) {
                String strMID = pref.getString(KEY_OTHER_DEVICES_MID_PREFIX + i, null);
                String strModel = pref.getString(KEY_OTHER_DEVICES_MODEL_PREFIX + i, null);
                if (!TextUtils.isEmpty(strMID) && !TextUtils.isEmpty(strModel)) {
                    strDevices.add(strMID);
                    strDevices.add(strModel);
                }
            }
        }
        return strDevices;
    }
    
    public static void
    _SetOtherDevices(Context ctx, ArrayList<String> strDevices)
    {
        final SharedPreferences pref = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = pref.edit();
        final int nOld = pref.getInt(KEY_OTHER_DEVICES_NUM, 0);
        final int N = strDevices.size() / 2;
        ed.putInt(KEY_OTHER_DEVICES_NUM, N);
        for (int i = 0; i < N; i++) {
            String strMID = strDevices.get(i * 2);
            String strModel = strDevices.get(i * 2 + 1);
            ed.putString(KEY_OTHER_DEVICES_MID_PREFIX + i, strMID);
            ed.putString(KEY_OTHER_DEVICES_MODEL_PREFIX + i, strModel);
        }

        for (int i = N; i < nOld; i++) {
            ed.remove(KEY_OTHER_DEVICES_MID_PREFIX + i);
            ed.remove(KEY_OTHER_DEVICES_MODEL_PREFIX + i);
        }
        Wrapper.SharedPreferencesEditorApply(ed);
    }
    
    public static void
    AddOtherDevice(Context ctx, String strMID, String strModel)
    {
        if (TextUtils.isEmpty(strMID) || 
            TextUtils.isEmpty(strModel)) return;
        
        ArrayList<String> strDevices = GetOtherDevices(ctx);
        final int N = strDevices.size() / 2;
        for (int i = 0; i < N; i++) {
            String strMID2 = strDevices.get(i * 2);
            String strModel2 = strDevices.get(i * 2 + 1);
            if (strMID2.equals(strMID) && 
                strModel2.equals(strModel))
                return;
        }
        
        strDevices.add(strMID);
        strDevices.add(strModel);
        _SetOtherDevices(ctx, strDevices);
    }
    
    public static void
    RemoveOtherDevice(Context ctx, String strMID)
    {
        if (TextUtils.isEmpty(strMID)) return;
        
        ArrayList<String> strDevices = GetOtherDevices(ctx);
        final int N = strDevices.size() / 2;
        for (int i = 0; i < N; i++) {
            String strMID2 = strDevices.get(i * 2);
            if (strMID2.equals(strMID)) {
                strDevices.remove(i * 2);
                strDevices.remove(i * 2 + 1);
                return;
            }
        }
    }
}
